package com.baize.baizeapp.test

import android.content.Context
import android.content.Intent
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baize.baizeapp.R

class RecyclerViewActivity : AppCompatActivity() {

    lateinit var recyclerView: RecyclerView
    var datas = arrayListOf<String>()
    val mAdapter: TestAdapter by lazy {
        TestAdapter()
    }

    var mCurPos = -1

    companion object {
        fun open(context: Context) {
            context.startActivity(
                Intent(context, RecyclerViewActivity::class.java)
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = mAdapter

        recyclerView.addOnChildAttachStateChangeListener(object :
            RecyclerView.OnChildAttachStateChangeListener {
            override fun onChildViewAttachedToWindow(view: View) {
                Log.i(
                    "yanze",
                    "onChildViewAttachedToWindow: 关联 ${recyclerView.getChildAdapterPosition(view)}"
                )
            }

            override fun onChildViewDetachedFromWindow(view: View) {
                Log.i(
                    "yanze",
                    "onChildViewAttachedToWindow: 移除 ${recyclerView.getChildAdapterPosition(view)}"
                )
            }
        })
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    autoPlayVideo(recyclerView)
                }
            }
        })

        repeat(12) {
            datas.add("$it")
        }
        mAdapter.notifyDataSetChanged()
    }

    fun onRefreshItem(v: View) {
        datas[5] = "刷"
        mAdapter.notifyItemChanged(5)
    }

    fun autoPlayVideo(view: RecyclerView?) {
        if (view == null) return
        //遍历RecyclerView子控件,如果mPlayerContainer完全可见就开始播放
        for (i in 0 until view.childCount) {
            val itemView: View = view.getChildAt(i) ?: continue
            val rect = Rect()
            itemView.getLocalVisibleRect(rect)
            //获取item的高度
            val height: Int = itemView.height ?: 0
            if (rect.top == 0 && rect.bottom == height) {
                val position = recyclerView.getChildAdapterPosition(itemView)
                val viewHolder = recyclerView.getChildViewHolder(itemView)
                Log.i("yanze_feed", "开始播放第：${position} (${i}) 个笔记！")
                if (position >= 0) {
                    startPlay(viewHolder as TestAdapter.TestViewHolder, position)
                }
                break
            }
        }
    }

    fun startPlay(viewHolder: TestAdapter.TestViewHolder, position: Int) {
        if (mCurPos == position) return
        datas[position] = "播"
        mAdapter.bindViewHolder(viewHolder,position)
    }

    inner class TestAdapter : RecyclerView.Adapter<TestAdapter.TestViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.item_test, parent, false)
            return TestViewHolder(view)
        }

        override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
            holder.textView.text = "${datas[position]}"
        }

        override fun getItemCount(): Int {
            return datas.size
        }

        inner class TestViewHolder(var v: View) : RecyclerView.ViewHolder(v) {
            lateinit var textView: TextView

            init {
                textView = v.findViewById(R.id.textView)
            }
        }
    }
}